package chucnang;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;



import net.miginfocom.swing.MigLayout;

import com.toedter.calendar.JDateChooser;

import form.DSJ_FormMuaHang;

import trangchu.TrangChu;

public class TraTien extends JPanel implements ActionListener {

	private JPanel plNorth,plCenter,plSouth;
	private JButton btnXem,btnXemToanBo;
	private JTextField txtTongConLai,txtTongDaTra,txtTongSoTien;
	public JDateChooser dateStart;
	public JDateChooser dateEnd;
	public JTable table;
	public DefaultTableModel model;
	private TrangChu trangchu;
	String[][] data = {};
    String[] colunms = {"Chứng Từ","Mã NPP","Nhà Phân Phối","Ngày","Số Tiền","Đã Trả","Còn Lại","Diễn Giải"};
    private JLabel lblgoithieu;
	public TraTien(TrangChu trangchu) {
		this.setLayout(new BorderLayout());
		plNorth = new JPanel();
		plNorth.setForeground(Color.BLUE);
		plNorth.setBorder(new EmptyBorder(5, 5, 5, 5));
		add(plNorth,BorderLayout.NORTH);
		plNorth.setLayout(new MigLayout("", "[][][grow]", "[][]"));
		
		//this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blue), "Danh Sách Công Nợ"));
		this.setBorder(new TitledBorder("Danh Sách Công Nợ Trả Tiền"));
//		JLabel lblDanhSach = new JLabel("Danh Sách Công Nợ");
//		lblDanhSach.setFont(new Font("Tahoma", Font.BOLD, 12));	
//		lblDanhSach.setForeground(Color.blue);
//		plNorth.add(lblDanhSach, "cell 0 0,growx");
		
		btnXemToanBo = new JButton("Tất Cả",new ImageIcon("images/xem2.gif"));
		plNorth.add(btnXemToanBo, "cell 0 1");
		
		JLabel lblTu = new JLabel("Từ : ");
		plNorth.add(lblTu, "flowx,cell 0 1");
		
		dateStart = new JDateChooser();
		dateStart.setPreferredSize(new Dimension(180, 20));
		dateStart.setDateFormatString("dd/MM/yyyy");
		Date todayD=new Date(System.currentTimeMillis());
		dateStart.setDate(todayD);
		plNorth.add(dateStart, "cell 0 1,growx");		
		JLabel lblDen = new JLabel("Đến : ");
		plNorth.add(lblDen, "cell 1 1,alignx trailing");
		
		dateEnd = new JDateChooser();
		dateEnd.setPreferredSize(new Dimension(180, 20));
		dateEnd.setDateFormatString("dd/MM/yyyy");
		Date todayD1=new Date(System.currentTimeMillis());
		dateEnd.setDate(todayD1);
		plNorth.add(dateEnd, "flowx,cell 2 1");
		
		btnXem = new JButton("Xem",new ImageIcon("images/xem1.gif"));
		plNorth.add(btnXem, "cell 2 1");			
		
		plCenter = new JPanel();
		add(plCenter,BorderLayout.CENTER);
		plCenter.setLayout(new BorderLayout());
    	JScrollPane scrollPane = new JScrollPane();
    	plCenter.add(scrollPane);
		
    	model = new DefaultTableModel(data, colunms);
    	table = new JTable(model);
    	table.getColumnModel().getColumn(0).setPreferredWidth(60);
    	table.getColumnModel().getColumn(1).setPreferredWidth(60);
		table.getColumnModel().getColumn(2).setPreferredWidth(250);
		table.getColumnModel().getColumn(3).setPreferredWidth(100);
		table.getColumnModel().getColumn(7).setPreferredWidth(200);
		scrollPane.setViewportView(table);

		
		plSouth = new JPanel();
		add(plSouth,BorderLayout.SOUTH);
		plSouth.setLayout(new MigLayout("", "[][][][][][][][][][][][][][][][][][][][][][][grow][][grow][][][][][grow][grow][grow][grow]", "[][]"));	
		
		JLabel lblTongSoTien = new JLabel("Tổng Số Tiền :");
		plSouth.add(lblTongSoTien, "cell 32 0,alignx right");
		
		txtTongSoTien = new JTextField();
		plSouth.add(txtTongSoTien, "cell 32 0,alignx right");
		txtTongSoTien.setColumns(12);
		
		JLabel lblTongDaTra = new JLabel("Tổng Đã Trả :");
		plSouth.add(lblTongDaTra, "cell 32 0,alignx right");
		
		txtTongDaTra = new JTextField();
		plSouth.add(txtTongDaTra, "cell 32 0,alignx right");
		txtTongDaTra.setColumns(12);
		
		JLabel lblTongConLai = new JLabel("Tổng Còn Lại :");
		plSouth.add(lblTongConLai, "cell 32 0,alignx right");
		
		txtTongConLai = new JTextField();
		plSouth.add(txtTongConLai, "cell 32 0,alignx right");
		txtTongConLai.setColumns(12);
		
		lblgoithieu = new JLabel("Phần mềm quản lý bán hàng ShopM                  ");
		lblgoithieu.setFont(new Font("Tahoma", 0, 10));
		lblgoithieu.setFont(new Font("Tahoma", 3, 11));		
		lblgoithieu.setForeground(Color.blue);
		plSouth.add(lblgoithieu, "cell 32 1,alignx right");

		JPanel plSouthgio= new JPanel();
		plSouthgio.setLayout(new FlowLayout(FlowLayout.RIGHT));
		plSouth.add(plSouthgio, "cell 32 1,alignx right");
		final JLabel lblgioserver = new JLabel();
		plSouthgio.add(lblgioserver);
		 final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a | dd/MM/yyyy");
	        Timer time = new Timer(1000, new ActionListener()
	        {
	            public void actionPerformed(ActionEvent e)
	            {
	                Date d = new Date(System.currentTimeMillis());
	                lblgioserver.setText(sdf.format(d.getTime()));
	            }
	        });
	        time.start();
	        
	        //setEditable
	        
	        txtTongSoTien.setEditable(false);
	        txtTongDaTra.setEditable(false);
	        txtTongConLai.setEditable(false);
	        
	        txtTongSoTien.setText("0");
	        txtTongDaTra.setText("0");
	        txtTongConLai.setText("0");
	        //sukien
	        btnXem.addActionListener(this);
	        btnXemToanBo.addActionListener(this);
	}
	
	//so tien
	public void getInfoSoTien(){
		Integer a = 0;	
		for (int i = 0; i < table.getModel().getRowCount(); i++) {
			a = a +Integer.parseInt((String) table.getModel().getValueAt(i, 4));
		}
		txtTongSoTien.setText(a.toString());	
	}
	//da tra
	public void getInfoDaTra(){
		Integer a = 0;	
		for (int i = 0; i < table.getModel().getRowCount(); i++) {
			a = a +Integer.parseInt((String) table.getModel().getValueAt(i, 5));
		}
		txtTongDaTra.setText(a.toString());	
	}
	//con lai
	public void getInfoConLai(){
		Integer a = 0;	
		for (int i = 0; i < table.getModel().getRowCount(); i++) {
			a = a +Integer.parseInt((String) table.getModel().getValueAt(i, 6));
		}
		txtTongConLai.setText(a.toString());	
	}
	public void listtoanbo(){
		DSJ_FormMuaHang b = new DSJ_FormMuaHang();
		Vector list = b.getTraTien();
		Vector list2;
		model.getDataVector().removeAllElements();
		for (int i = 0; i < list.size(); i++) {
			list2 = (Vector) list.get(i);
			Object[] oPerson = {
					list2.get(0).toString(),
					list2.get(1).toString(),
					list2.get(2).toString(),
					list2.get(3).toString(),
					list2.get(4).toString(),
					list2.get(5).toString(),
					list2.get(6).toString(),
					list2.get(7).toString()};
			
			model.insertRow(0, oPerson);
			
		}
		model.fireTableDataChanged();
	}
	public void listxem(){
		DSJ_FormMuaHang b = new DSJ_FormMuaHang();
		Vector list = b.getTraTienxem(this);
		Vector list2;
		model.getDataVector().removeAllElements();
		for (int i = 0; i < list.size(); i++) {
			list2 = (Vector) list.get(i);
			Object[] oPerson = {
					list2.get(0).toString(),
					list2.get(1).toString(),
					list2.get(2).toString(),
					list2.get(3).toString(),
					list2.get(4).toString(),
					list2.get(5).toString(),
					list2.get(6).toString(),
					list2.get(7).toString()};
			
			model.insertRow(0, oPerson);
			
		}
		model.fireTableDataChanged();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnXem){
			listxem();
			getInfoSoTien();
			getInfoDaTra();
			getInfoConLai();
		}
		if(e.getSource() ==btnXemToanBo){
			listtoanbo();
			getInfoSoTien();
			getInfoDaTra();
			getInfoConLai();
		}
		
	}
	
}
